package com.ruoyi.web.controller.train;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.Specialty;
import com.ruoyi.system.service.ISpecialtyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 专业Controller
 *
 * @author dp
 * @date 2023-11-27
 */
@Api(tags = {"专业"})
@RestController
@RequestMapping("/dict/specialty")
public class SpecialtyController extends BaseController {
    @Autowired
    private ISpecialtyService SpecialtyService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询专业列表
     */
    @ApiOperation(value = "专业列表", notes = "查询专业列表详情", tags = {"专业管理"})
    //@PreAuthorize("@ss.hasPermi('dict:specialty:list')")
    @GetMapping("/list")
    public TableDataInfo list(Specialty Specialty) {
        startPage();
        List<Specialty> list = SpecialtyService.selectSpecialtyList(Specialty);
        return getDataTable(list);
    }

    /**
     * 导出专业列表
     */
    @ApiOperation(value = "专业列表导出", notes = "导出专业列表", tags = {"专业管理"})
    //@PreAuthorize("@ss.hasPermi('dict:specialty:export')")
    @Log(title = "专业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Specialty Specialty) {
        List<Specialty> list = SpecialtyService.selectSpecialtyList(Specialty);
        ExcelUtil<Specialty> util = new ExcelUtil<Specialty>(Specialty.class);
        util.exportExcel(response, list, "专业数据");
    }

    /**
     * 获取专业详细信息
     */
    @ApiOperation(value = "获取专业详细信息", notes = "获取专业详细信息详情", tags = {"专业管理"})
    //@PreAuthorize("@ss.hasPermi('dict:specialty:query')")
    @GetMapping(value = "/{SpecialtyId}")
    public AjaxResult getInfo(@PathVariable("SpecialtyId") Long SpecialtyId) {
        return success(SpecialtyService.selectSpecialtyBySpecialtyId(SpecialtyId));
    }

    /**
     * 新增专业
     */
    @ApiOperation(value = "新增专业", notes = "新增专业详情", tags = {"专业管理"})
    //@PreAuthorize("@ss.hasPermi('dict:specialty:add')")
    @Log(title = "专业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody Specialty Specialty) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        SpecialtyService.completeData(Specialty, userId, 1);
        return toAjax(SpecialtyService.insertSpecialty(Specialty));
    }

    /**
     * 修改专业
     */
    @ApiOperation(value = "修改专业", notes = "修改专业详情", tags = {"专业管理"})
    //@PreAuthorize("@ss.hasPermi('dict:specialty:edit')")
    @Log(title = "专业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request,@RequestBody Specialty Specialty) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        SpecialtyService.completeData(Specialty, userId, 2);
        return toAjax(SpecialtyService.updateSpecialty(Specialty));
    }

    /**
     * 删除专业
     */
    @ApiOperation(value = "删除专业", notes = "删除专业详情", tags = {"专业管理"})
    //@PreAuthorize("@ss.hasPermi('dict:specialty:remove')")
    @Log(title = "专业", businessType = BusinessType.DELETE)
    @DeleteMapping("/{SpecialtyIds}")
    public AjaxResult remove(@PathVariable Long[] SpecialtyIds) {
        return toAjax(SpecialtyService.deleteSpecialtyBySpecialtyIds(SpecialtyIds));
    }
}
